/*
 * Copyright (c) 2017 Qunar.com. All Rights Reserved.
 */
package com.guoyu.project.web.admin;

import java.util.Map;

import javax.annotation.Resource;

import com.guoyu.project.model.School;
import com.guoyu.project.model.enums.CollegeType;
import com.guoyu.project.service.ProvinceService;
import com.guoyu.project.service.SchoolService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Article;
import com.guoyu.project.model.enums.ArticleMinorType;
import com.guoyu.project.model.enums.ArticleType;
import com.guoyu.project.service.ArticleService;
import com.guoyu.project.service.FileUploadService;

/**
 * @author: libo Date: 17-5-26 Time: 下午3:45
 * @version: \$Id$
 */
@Controller
@RequestMapping("admin/school")
public class SchoolController extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(NewsController.class);

    @Resource
    private SchoolService schoolService;
    @Resource
    private ProvinceService provinceService;

    @Resource
    private FileUploadService fileUploadService;

    @RequestMapping("index")
    public String index() {
        return "admin/school/index";
    }

    @RequestMapping("list")
    @ResponseBody
    public Pagination<School> list(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit, String name) {
        Map<String, Object> params = Maps.newHashMap();
        if (StringUtils.isNotBlank(name)) {
            params.put("name", name);
        }
        return schoolService.querySchools(offset, limit, params);
    }

    /**
     * 跳到添加今日资讯填写页
     *
     * @return
     */
    @RequestMapping("add")
    public String add(Model model) {
        model.addAttribute("typeList", CollegeType.values());
        model.addAttribute("provinceList", provinceService.queryAll());
        return "admin/school/add";
    }

    /**
     * 保存今日资讯信息
     *
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public APIResponse save(@RequestParam(value = "file", required = false) MultipartFile picture, School school) {
        logger.info("提交保存学校信息：{}", JsonUtil.encode(school));
        if (StringUtils.isBlank(school.getName())) {
            return APIResponse.returnFail("学校名称不能为空");
        }
        String imgPath = fileUploadService.upload(picture);
        school.setPicture(imgPath);
        schoolService.saveSchool(school);
        return APIResponse.returnSuccess();
    }

    /**
     * 跳到修改今日资讯信息页面
     *
     * @return
     */
    @RequestMapping("edit")
    public String edit(int schoolId, Model model) {
        School school = schoolService.queryById(schoolId);
        model.addAttribute("school", school);
        model.addAttribute("typeList", CollegeType.values());
        model.addAttribute("provinceList", provinceService.queryAll());
        return "admin/school/edit";
    }

    /**
     * 更新今日资讯信息
     *
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public APIResponse update(@RequestParam(value = "file", required = false) MultipartFile picture, School school) {
        logger.info("提交修改学校信息：{}", JsonUtil.encode(school));
        String imgPath = StringUtils.defaultIfBlank(fileUploadService.upload(picture), school.getPicture());
        school.setPicture(imgPath);
        schoolService.updateSchool(school);
        return APIResponse.returnSuccess();
    }

    /**
     * 删除今日资讯信息
     *
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public APIResponse delete(int schoolId) {
        schoolService.deleteById(schoolId);
        return APIResponse.returnSuccess();
    }
}
